#include "RoomManager.h"
#include "DBManager.h"

quint16 RoomManager::createRoom() {
    quint16 roomId = nextRoomId++;

    AbstractRoom* room = new AbstractRoom(roomId, this);
    roomMap[roomId] = room;

    qDebug() << "Created room:" << roomId;
    return roomId;
}

void RoomManager::deleteRoom(quint16 roomId) {
    if (roomMap.contains(roomId)) {
        delete roomMap[roomId];
        roomMap.remove(roomId);
        qDebug() << "Deleted room:" << roomId;
    } else {
        qDebug() << "Room not found for deletion:" << roomId;
    }
}

bool RoomManager::addClientToRoom(const quint16& roomId, AbstractUser*& user, AbstractRoom*& getRoom) {
    if (!roomMap.contains(roomId)) {
        qDebug() << "Room not found:" << roomId;
        return false;
    }

    getRoom = roomMap[roomId];
    getRoom->addClient(user);

    qDebug() << "Added client" << user->getID() << "to room" << roomId;

    return true;
}

bool RoomManager::removeClientFromRoom(const quint16& roomId, const quint16& userId, AbstractRoom*& getRoom) {
    if (!roomMap.contains(roomId)) {
        qDebug() << "Room not found:" << roomId;
        return false;
    }

    getRoom = roomMap[roomId];
    getRoom->removeClient(userId);

    qDebug() << "Removed client" << userId << "from room" << roomId;
    return true;
}

QVector<quint16> RoomManager::getClientIDsInRoom(const quint16& roomId) const {
    if (!roomMap.contains(roomId)) {
        qDebug() << "Room not found:" << roomId;
        return {};
    }

    const AbstractRoom* room = roomMap[roomId];
    return room->getClientIDs();
}